package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.bo.AdminIssueCreateBO;
import com.cskaoyan.app.bean.bo.AdminIssueUpdateBO;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketIssue;
import com.cskaoyan.app.bean.po.MarketIssueExample;
import com.cskaoyan.app.mapper.MarketIssueMapper;
import com.cskaoyan.app.service.AdminIssueService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/29 20:32
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public CommonData<MarketIssue> list(BasePageInfo basePageInfo, String question) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(StringsUtil.getOrderClause(basePageInfo.getSort(),basePageInfo.getOrder()));
        MarketIssueExample.Criteria criteria = example.createCriteria();
        if(!StringsUtil.isEmpty(question)){
            criteria.andQuestionLike("%"+question+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(example);
        PageInfo<MarketIssue> marketIssuePageInfo = new PageInfo<>(marketIssues);
        return CommonData.data(marketIssuePageInfo);
    }

    @Override
    public MarketIssue update(AdminIssueUpdateBO adminIssueUpdateBO) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(adminIssueUpdateBO.getId());
        marketIssue.setQuestion(adminIssueUpdateBO.getQuestion());
        marketIssue.setAnswer(adminIssueUpdateBO.getAnswer());
        marketIssue.setAddTime(adminIssueUpdateBO.getAddTime());
        marketIssue.setUpdateTime(new Date());
        marketIssue.setDeleted(adminIssueUpdateBO.getDeleted());
        Integer i = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        if(i != 1){return null;}
        marketIssue = marketIssueMapper.selectByPrimaryKey(adminIssueUpdateBO.getId());
        return marketIssue;
    }

    @Override
    public MarketIssue create(AdminIssueCreateBO adminIssueCreateBO) {
        MarketIssue marketIssue = new MarketIssue();
        Date date = new Date();
        marketIssue.setQuestion(adminIssueCreateBO.getQuestion());
        marketIssue.setAnswer(adminIssueCreateBO.getAnswer());
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        marketIssue.setDeleted(false);
        Integer row = marketIssueMapper.insertSelective(marketIssue);
        if(row != 1){return null;}
        marketIssue = marketIssueMapper.selectByPrimaryKey(marketIssue.getId());
        return marketIssue;
    }

    @Override
    public Integer delete(MarketIssue marketIssue) {
        marketIssue.setDeleted(true);
        marketIssue.setUpdateTime(new Date());
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketIssue.getId());
        Integer i = marketIssueMapper.updateByExampleSelective(marketIssue, example);
        return i;
    }


}
